//
//  HomesViewController.swift
//  DouYuZB
//
//  Created by James on 2017/10/13.
//  Copyright © 2017年 James. All rights reserved.
//

import UIKit
import AVKit

private let kTitleViewH:CGFloat = 40

class HomesViewController: UIViewController {
    // MARK: - 懒加载属性
    fileprivate lazy var pageTitleView: PageTitleView = {[weak self] in
        let frame = CGRect(x: 0, y: kStatusBarH + kNavigationBarH, width: kScreenW , height: kTitleViewH)
        let titles = ["推荐","游戏","娱乐","趣玩"]
        let titleView = PageTitleView(frame: frame, titles: titles)
        titleView.delegate = self
        titleView.backgroundColor = UIColor.clear
        return titleView
    }()
    
    fileprivate lazy var pageContentView: PageContentView = {[weak self] in

        
        //1.确定内容的frame
        let frame = CGRect(x: 0, y: kStatusBarH + kNavigationBarH + kTitleViewH, width: kScreenW, height: kScreenH-(kStatusBarH + kNavigationBarH + kTitleViewH+kTabBarH))
        //2.确定所有的子控制器
        var childVCs = [UIViewController]()
        
        childVCs.append(RecommondViewController())
        childVCs.append(GameViewController())
        
        for _ in 0..<2 {
            let vc = UIViewController()
            vc.view.backgroundColor = UIColor(r: CGFloat(arc4random_uniform(255)), g: CGFloat(arc4random_uniform(255)), b: CGFloat(arc4random_uniform(255)))
            vc.view.frame.size = frame.size
            childVCs.append(vc)
        }
        
        let view = PageContentView(frame: frame, childVCs: childVCs, parentVC: self)
        view.delegate = self

        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        /**
         1.封装PageTitleView
         2.封装PageContentView
         3.处理PageTitleView & PageContentView的逻辑
         */
    }
    
    func textPalyVoice()  {
        // 创建嗓音，指定嗓音不存在则返回nil
        let voice = AVSpeechSynthesisVoice(language: "zh-CN");
        // 创建语音合成器
        let synthesizer = AVSpeechSynthesizer()
         // 实例化发声的对象
        let utterance = AVSpeechUtterance(string: "世上只有妈妈好，有妈的孩子像块宝。投进妈妈的怀抱，幸福哪里找。没妈的孩子像根草")
        utterance.voice = voice
        utterance.rate = 0.5 // 语速
        // 朗读的内容
        synthesizer.speak(utterance)
    }
}

// MARK: - 设置UI
extension HomesViewController {
    fileprivate func setupUI() {
        //不需要调整UIScrollView的内边距
        if #available(iOS 11, *) {
            //iOS11之后需要到 UIScrollView 初始化的时候
            //设置contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
           automaticallyAdjustsScrollViewInsets = false //iOS11之前
        }
        
        setupNavigationBar()
        //1.添加titlesView
        view.addSubview(pageTitleView)
        //2.添加contentsView
        view.addSubview(pageContentView)
    }
    fileprivate func setupNavigationBar() {
        //设置左边item
        let btn = UIButton()
        btn.setImage(UIImage(named:"logo"), for: .normal)
        btn.sizeToFit()
        navigationItem.leftBarButtonItem = UIBarButtonItem(normalImg: "logo")
    
        //设置右边itmes
        let historyItem = UIBarButtonItem(normalImg: "image_my_history", highlightImg: "Image_my_history_click",size:CGSize(width: 40, height: 40))
        
        let searchItem = UIBarButtonItem(normalImg: "btn_search", highlightImg: "btn_search_clicked",size:CGSize(width: 40, height: 40))
        
        let qrcode = UIBarButtonItem(normalImg: "Image_scan", highlightImg: "Image_scan_click",size:CGSize(width: 40, height: 40))
        
        navigationItem.rightBarButtonItems = [historyItem,searchItem,qrcode]
    }
    

}
// MARK: - delegate
extension HomesViewController: PageTitleViewDelegate {
    func pageTitleView(titleView: PageTitleView, selectIndex index: Int) {
        pageContentView.setCurrentIndex(currentIndex: index)
    }
}
extension HomesViewController: PageContentViewDelegate {
    func pageContentView(pageContentView: PageContentView, progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
        pageTitleView.setTitleWithProgress(progress: progress, sourceIndex: sourceIndex, targetIndex: targetIndex)
    }
}
